package com.jadefashion.service.impl;

import com.jadefashion.health.entity.Result;
import com.jadefashion.health.resources.DateUtils;
import com.jadefashion.health.resources.HttpClient;
import com.jadefashion.mapper.MemberMapper;
import com.jadefashion.mapper.OrderMapper;
import com.jadefashion.mapper.ReportMapper;
import com.jadefashion.service.MemberService;
import com.jadefashion.service.ReportService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
@Transactional
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Map findBusinessData() {
        HashMap<String, Object> map = new HashMap<>();
        Date nowDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String reportDate=sdf.format(nowDate);
        map.put("reportDate",reportDate);//今日时间
        //todayNewMember :0,//今日新增会员数
        //todayOrderNumber :0,//今日预约数
        //todayVisitsNumber :0,//今日到诊数
        int todayNewMember=memberMapper.findMemberCountByDate(reportDate,reportDate);
        int todayOrderNumber=orderMapper.findOrderCountByDate(reportDate,reportDate);
        int todayVisitsNumber=orderMapper.findOrderFinishCount(reportDate,reportDate);
        map.put("todayNewMember",todayNewMember);
        map.put("todayOrderNumber",todayOrderNumber);
        map.put("todayVisitsNumber",todayVisitsNumber);
        //totalMember :0,//总会员数
        int totalMember=memberMapper.totalMember();
        map.put("totalMember",totalMember);

        //thisWeekNewMember :0,//本周新增会员数
        //thisWeekOrderNumber :0,//本周预约数
        //thisWeekVisitsNumber :0,//本周到诊数
        Date firstDayOfWeek = DateUtils.getFirstDayOfWeek(nowDate);
        Date lastDayOfWeek = DateUtils.getLastDayOfWeek(nowDate);
        String firstDayOfWeekStr=sdf.format(firstDayOfWeek);
        String lastDayOfWeekStr=sdf.format(firstDayOfWeek);
        int thisWeekNewMember=memberMapper.findMemberCountByDate(firstDayOfWeekStr,lastDayOfWeekStr);
        int thisWeekOrderNumber = orderMapper.findOrderCountByDate(firstDayOfWeekStr,lastDayOfWeekStr);
        int thisWeekVisitsNumber = orderMapper.findOrderFinishCount(firstDayOfWeekStr,lastDayOfWeekStr);
        map.put("thisWeekNewMember",thisWeekNewMember);
        map.put("thisWeekOrderNumber",thisWeekOrderNumber);
        map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        //thisMonthNewMember :0,//本月新增会员数
        //thisMonthOrderNumber :0,//本月预约数
        //thisMonthVisitsNumber :0,//本月到诊数
        Date firstDayOfMOnth= DateUtils.getFirstDayDateOfMonth(nowDate);
        Date lastDayOfMOnth = DateUtils.getLastDayOfWeek(nowDate);
        String firstDayOfMOnthStr=sdf.format(firstDayOfMOnth);
        String lastDayOfMOnthStr=sdf.format(lastDayOfMOnth);
        int thisMonthNewMember=memberMapper.findMemberCountByDate(firstDayOfMOnthStr,lastDayOfMOnthStr);
        int thisMonthOrderNumber = orderMapper.findOrderCountByDate(firstDayOfMOnthStr,lastDayOfMOnthStr);
        int thisMonthVisitsNumber = orderMapper.findOrderFinishCount(firstDayOfMOnthStr,lastDayOfMOnthStr);
        map.put("thisMonthNewMember",thisMonthNewMember);
        map.put("thisMonthOrderNumber",thisMonthOrderNumber);
        map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);

        //hotSetmeal :[//显示4个热门套餐
        List<Map> hotSetmeal=orderMapper.findHotSetmeal();
        map.put("hotSetmeal",hotSetmeal);
        //{name:"粉红珍爱(女)升级TM12项筛查体检套餐",setmeal_count:5,proportion:0.4545},
        //{name:"阳光爸妈升级肿瘤12项筛查体检套餐",setmeal_count:2,proportion:0.1818},
        //{name:"珍爱高端升级肿瘤12项筛查",setmeal_count:2,proportion:0.1818},
        //{name:"孕前检查套餐",setmeal_count:1,proportion:0.0909}
        //查询预约总数
        int totalcounts=orderMapper.totalOrder();
        map.put("totalcounts",totalcounts);
        return map;
    }

    @Override
    public List<Map> setmemberAge() {
        List<Map> list=reportMapper.setmemberAge();
        return list;
    }

    @Override
    public List<Map> getSexNum() {
        List<Map> lista = reportMapper.getSexNum();
        return lista;
    }




    @Override
    public List<Map> findOrderNumber() {

        List<Map> list=reportMapper.findOrderNumber();
        return list;
    }

    @Override
    public Map getMemberReport() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH,-11);
        List<String> yearAndMonth=new ArrayList<>();
        List<Integer> memberNum=new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            String yearAndMonthStr=year+"-"+(month+1);
            yearAndMonth.add(yearAndMonthStr);
            String start=yearAndMonthStr+"-1";
            String end=yearAndMonthStr+"-31";
            int count=reportMapper.memberNumPerMonth(start,end);
            memberNum.add(count);
            c.add(Calendar.MONTH,1);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("yearAndMonth",yearAndMonth);
        map.put("memberNum",memberNum);
        return map;
    }

    /**
     * 会员折线图展示,有参数,根据参数数据展示,没参数,展示过去一年数据
     * @param time
     * @return
     */
    @Override
    public Map getMemberReport(Map time) throws Exception {
        //创建存储会员数量的集合
        List<Integer> num = new ArrayList<>();

        //创建存储日期的集合
        List<String> dat=new ArrayList<>();

        if (time==null||time.size()==0){
            //获取当前日期,使用localDate api
            LocalDate today = LocalDate.now();

            //当前日期向前一年
            LocalDate date = today.minusYears(1);
            for (int i = 0; i < 12; i++) {
                //转换日期为string
                DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyy-MM");
                String format = date.format(dtf);

                //将每个日期添加进日期集合
                dat.add(format);

                //获取每月起始
                String beginDate=format+"-1";
                //获取每月结束
                String endDate=format+"-31";

                //发送请求,查询每月会员数量
                Integer count = memberMapper.memberNumPerMonth(beginDate,endDate);

                //添加每月会员数量进集合
                num.add(count);

                date = date.plusMonths(1);
            }
        }else {
            //获取传递过来的开始日期
            String first = (String) time.get("firstDay");
            Date date = DateUtils.parseString2Date(first);
            //调用方法,获得所选开始月份日期时间
            Date firstDay = MonthAdd(date);
            String beginDay = DateUtils.parseDate2String(firstDay);

            //调用方法,获得所选月份的结束时间
            String end = (String) time.get("endDay");
            String overDay = DateUtils.parseDate2String(MonthAdd(DateUtils.parseString2Date(end)));
            //获取每个月的数据
            List<String> monthBetween = DateUtils.getMonthBetween(beginDay, overDay, "yyyy-MM");
            //遍历月份集合
            for (String mon : monthBetween) {
                //将月份数据添加进集合
                dat.add(mon);
                //获取每月起始
                String beginDate=mon+"-1";
                //获取每月结束
                String endDate=mon+"-31";

                //传递数据查询
                Integer count = memberMapper.memberNumPerMonth(beginDate,endDate);
                //将数量添加进集合
                num.add(count);
            }
        }

        //创建一个map集合封装日期和数量
        Map map = new HashMap();
        map.put("num",num);
        map.put("dat",dat);
        return map;

    }

    //将参数增加一个月
    private Date MonthAdd(Date date3){
        if (date3==null){
            return null;
        }else {//创建日期对象
            Calendar first=Calendar.getInstance();
            //将传递的时间传递给日期对象
            first.setTime(date3);
            //将传递的时间增加一个月
            first.add(Calendar.MONTH,1);
            //取出增加一个月后的时间
            Date time1 = first.getTime();

            return time1;}
    }
}
